#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char str[20];
LL get(char s[],LL l,LL r){
   LL sum=0;
   for(LL i=l;i<=r;i++){
      sum=sum*10+(s[i]-'0');
   }
   return sum;
}
char div(LL l){
    return char('0'+l);
}
LL check(char p[],LL h,LL m,LL pos,LL len){

   for(LL i=1;i<=len;i++){
     if(p[i]=='#') return 0;
   }
   LL h1=get(p,1,pos-1);
   LL m1=get(p,pos+1,len);
   if(h1<h&&m1<m) return 1;
   else return 0;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--){
     LL h,m;cin>>h>>m;
     cin>>(str+1);
     LL len=strlen(str+1);
     LL pos=0;
     for(LL i=1;i<=len;i++){
        if(str[i]==':'){
            pos=i;break;
        }
     }
     LL hour=get(str,1,pos-1);
     LL minu=get(str,pos+1,len);
     bool flag=1;
     for(LL i=hour;flag==1&&i<=h-1;i++){
        LL rrrr=0;
        if(i==hour) rrrr=minu;
        else rrrr=0;
        for(LL j=rrrr;flag==1&&j<m;j++){
            char now[10],p[10];LL tot=0;LL cnt=0;
            stack<char>s;
            LL temp=j;
            if(temp<10){
                if(temp==0){
                    s.push('0');s.push('0');
                }
                else{
                    s.push(div(temp%10));s.push('0');
                }
            }
            else{
                while(temp>0){
                    s.push(div(temp%10));
                    temp/=10;
                }
            }
            s.push(':');
            temp=i;
            if(temp<10){
                if(temp==0){
                    s.push('0');s.push('0');
                }
                else{
                    s.push(div(temp%10));s.push('0');
                }
            }
            else{
                while(temp>0){
                s.push(div(temp%10));
                temp/=10;
                }
            }
            while(!s.empty()){
                    now[++cnt]=s.top();
                    s.pop();
            }
            for(LL i=cnt;i>=1;i--){
                if(now[i]==':') p[++tot]=':';
                else if(now[i]=='0') p[++tot]='0';
                else if(now[i]=='1') p[++tot]='1';
                else if(now[i]=='2') p[++tot]='5';
                else if(now[i]=='5') p[++tot]='2';
                else if(now[i]=='8') p[++tot]='8';
                else p[++tot]='#';
            }
            LL ppp=0;
            for(LL i=1;i<=tot;i++){
                if(p[i]==':'){
                    ppp=i;break;
                }
            }
            LL res=check(p,h,m,ppp,tot);
           /// debug(res);
            if(res==1){
                flag=0;
                for(LL i=1;i<=cnt;i++){
                    cout<<now[i];
                }
                cout<<"\n";
                break;
            }
        }
     }
     if(1==flag){
        cout<<"00:00"<<"\n";
     }
  }
return 0;
}
